//
//  HOTOrderPreviewViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14/11/4.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTOrderPreviewViewController.h"
#import "ZZZUtils.h"
#import "HOTCostPopupHandler.h"

@interface HOTOrderPreviewViewController () <ZZZPopupViewDelegate>

@property (nonatomic,strong)NSDictionary *orderInfo;
@property(nonatomic,copy)NSString *dateStr;
@property(nonatomic,copy)NSString *convId;
@property(nonatomic,copy)NSString *targetId;
@end

@implementation HOTOrderPreviewViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil orderInfo:(NSDictionary *) orderInfo {
    
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.orderInfo = orderInfo;
        
        self.convId = [self.orderInfo objectForKey:@"convId"];
        self.targetId = [self.orderInfo objectForKey:@"targetId"];
        return self;
    }
    
    return nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNaviTitle:@"発注書"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_message"]];
   
    [self initView];
}

-(void)initView {
    
    _orderNameLab.text = [self.orderInfo objectForKey:@"orderName"];
    _dateLab.text = [ZZZUtils formartDate:[NSDate date] withStyle:@"yyyy年MM月dd日"];
    self.dateStr = [ZZZUtils formartDate:[NSDate date] withStyle:@"yyyy-MM-dd"];
    _publisherNameLab.text = [self.orderInfo objectForKey:@"publisherName"];
    _publisherAddrLab.text = [self.orderInfo objectForKey:@"publisherAddr"];
    _reciverNameLab.text = [self.orderInfo objectForKey:@"reciverName"];
    _remarkLab.text = [self.orderInfo objectForKey:@"remark"];
    _priceLab.text =  [self.orderInfo objectForKey:@"price"];
    _zipCodeLab.text = [self.orderInfo objectForKey:@"publisherZipCode"];
    _phoneNumLab.text = [self.orderInfo objectForKey:@"publisherPhoneNum"];
    _faxNumLab.text = [self.orderInfo objectForKey:@"publisherFAXNum"];
    _workDateLab.text = [self.orderInfo objectForKey:@"workDate"];
    _payDateLab.text = [self.orderInfo objectForKey:@"payDate"];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)publishBtnClicked:(id)sender {
    
    
    
    ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:MSG_IS_CONFIRM delegate:self cancelButtonTitle:ALERT_BTN_CANCEL otherButtonTitle:ALERT_BTN_OK];
    popupView.tag = 101;
    [popupView showInWindow];
    
}

-(void)popupView:(ZZZPopupView *)popupView buttonKind:(ZZZPopupViewBtnKind)btnKind {
    [super popupView:popupView buttonKind:btnKind];
    if (popupView.tag == 101 && btnKind == ZZZPopupViewConfirmBtnKind) {
        
        __unsafe_unretained HOTOrderPreviewViewController *tempObj = self;
        [[HOTCostPopupHandler costPopupHandler] showWithPopupStyle:HOTCostPopupStyle4
                                                          userInfo:nil
                                                            cancel:nil
                                                           confirm:^{
                                                               
                                                              [tempObj requestUpdate];
                                                           }];
        
        
    }
}


-(void)requestUpdate {
    
    [self showLoading];

    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_PUBLISH_TIME:self.dateStr,
                                 REQ_KEY_TARGET_ID:self.targetId,
                                 REQ_KEY_PROJECT_NAME:_orderNameLab.text,
                                 REQ_KEY_CONV_ID:self.convId,
                                 REQ_KEY_COST_WITHOUT_TAX:_priceLab.text,
                                 REQ_KEY_PUBLISHER_REAL_NAME:_publisherNameLab.text,
                                 REQ_KEY_PUBLISHER_CODE:_zipCodeLab.text,
                                 REQ_KEY_PUBLISHER_ADDRESS:_publisherAddrLab.text,
                                 REQ_KEY_PUBLISHER_TEL:_phoneNumLab.text,
                                 REQ_KEY_PUBLISHER_FAX:_faxNumLab.text,
                                 REQ_KEY_RECEIVE_REAL_NAME:_reciverNameLab.text,
                                 REQ_KEY_PROJECT_TIME:_workDateLab.text,
                                 REQ_KEY_PAY_TIME:_payDateLab.text,
                                 REQ_KEY_REMARK:_remarkLab.text};
    
    __unsafe_unretained HOTOrderPreviewViewController *tempObj = self;
    [self requestWithURL:HOT_URL_ADD_ORDER Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^{
            
            NSString *url = [data objectForKey:@"contract_url"];
            NSString *orderId = [data objectForKey:@"contract_id"];
            
            if (tempObj.completion) {

                [tempObj dismissViewControllerAnimated:YES completion:^{
                    tempObj.completion(url, orderId);
                }];
            }

        }];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

@end
